<?php
namespace app\console\controller;
use app\console\controller\Auth;
use think\facade\Config;
use think\Db;
use app\service\controller\File;

/**
 * 首页设置
 * */
class Info extends Auth
{
    //行业资讯导航加载
    public function ml_list(){
        $lists=Db::table('tp_info')->paginate(15);
        $this->assign('active', 'info');
        $this->assign([
            'topbar' => ['行业资讯设置', '行业资讯导航设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //行业资讯修改
    public function ml_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //更新栏目
            $res=Db::table('tp_info')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_info')->where('id',$id)->find();

        $this->assign([
            'topbar' => ['行业资讯设置', '行业资讯导航设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }

    // 行业资讯设置
    public function ask_list(){
        $lists=Db::table('tp_info_par')
            ->where('info_id',1)
            ->order('sort')
            ->paginate(10);
        $this->assign('active','info');
        $this->assign([
            'topbar' => ['行业资讯设置', '加盟问答设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }

    //行业资讯添加
    public function ask_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $data['info_id']=1;
            $res=Db::table('tp_info_par')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $this->assign([
            'topbar' => ['行业资讯设置', '加盟问答设置','添加'],
        ]);
        return $this->fetch();
    }
    //行业资讯修改
    public function ask_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_info_par')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_info_par')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['行业资讯设置', '加盟问答设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }
    //餐饮知识加载
    public function know_list(){
        $lists=Db::table('tp_info_par')
            ->where('info_id',2)
            ->order('sort')
            ->paginate(10);
        $this->assign('active','info');
        $this->assign([
            'topbar' => ['行业资讯设置', '餐饮知识设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //餐饮知识添加
    public function know_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $data['info_id']=2;
            //更新栏目
            $res=Db::table('tp_info_par')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $this->assign([
            'topbar' => ['行业资讯设置', '餐饮知识设置','添加'],
        ]);
        return $this->fetch();
    }
    //修改
    public function know_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $res=Db::table('tp_info_par')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_info_par')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['行业资讯设置', '餐饮知识设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }
    //公司新闻加载
    public function state_list(){
        $lists=Db::table('tp_info_par')
            ->where('info_id',3)
            ->order('sort')
            ->paginate(10);
        $this->assign('active','info');
        $this->assign([
            'topbar' => ['行业资讯设置', '公司新闻设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    public function state_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $data['info_id']=3;
            //更新栏目
            $res=Db::table('tp_info_par')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $this->assign([
            'topbar' => ['行业资讯设置', '公司新闻设置','添加'],
        ]);
        return $this->fetch();
    }
    //修改
    public function state_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $res=Db::table('tp_info_par')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_info_par')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['行业资讯设置', '公司新闻设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }
}
